package com.itheima.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.itheima.common.ThreadF;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Component
@Slf4j
public class MyMetaObjecthandler implements MetaObjectHandler {

//    @Autowired
//    private HttpServletRequest request;

    /**
     * 插入操作，自动填充
     *
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
//        log.info("公共字段自动填充[insert]...");
//        log.info(metaObject.toString());
        //获取Session里面的id
        // Object q =  request.getSession().getAttribute("Q");
       // String uri = request.getRequestURI();

      //  int length = ThreadF.getCurrentId().toString().length();

        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("createUser", ThreadF.getCurrentId());
        metaObject.setValue("updateUser", ThreadF.getCurrentId());


       /* if (uri.contains("/shoppingCart") || uri.contains("/addressBook")) {
            metaObject.setValue("userId", ThreadF.getCurrentId());
        }*/


    }

    /**
     * 更新操作，自动填充
     *
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        // log.info("公共字段自动填充[update]...");
        //获取Session里面的id
        // Object q =  request.getSession().getAttribute("Q");
        log.info(metaObject.toString());

        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateUser", ThreadF.getCurrentId());
      /*  String uri = request.getRequestURI();
        if (uri.contains("/shoppingCart")) {
            metaObject.setValue("userId", ThreadF.getCurrentId());

        }*/
        //metaObject.setValue("updateUser",q);
    }
}
